package cn.wolfcode.service.impl;

import cn.wolfcode.common.exception.BusinessException;
import cn.wolfcode.domain.AccountLog;
import cn.wolfcode.domain.AccountTransaction;
import cn.wolfcode.domain.OperateIntergralVo;
import cn.wolfcode.mapper.AccountLogMapper;
import cn.wolfcode.mapper.AccountTransactionMapper;
import cn.wolfcode.mapper.UsableIntegralMapper;
import cn.wolfcode.service.IUsableIntegralService;
import cn.wolfcode.web.msg.IntergralCodeMsg;
import com.alibaba.fastjson.JSON;
import io.seata.rm.tcc.api.BusinessActionContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Slf4j
@Service
@Transactional
public class UsableIntegralServiceImpl implements IUsableIntegralService {

    @Autowired
    private UsableIntegralMapper usableIntegralMapper;
    @Autowired
    private AccountTransactionMapper accountTransactionMapper;
    @Autowired
    private AccountLogMapper accountLogMapper;

    @Override
    public void decrIntergral(OperateIntergralVo operateIntergralVo) {
        //往积分支付t_account_log表中插入数据
        AccountLog accountLog = new AccountLog();
        accountLog.setAmount(operateIntergralVo.getValue());
        accountLog.setPkValue(operateIntergralVo.getPk());
        accountLog.setInfo(operateIntergralVo.getInfo());
        accountLog.setGmtTime(new Date());
        accountLog.setType(AccountLog.TYPE_DECR);
        accountLogMapper.insert(accountLog);
        //减少积分
        int count = usableIntegralMapper.decrIntergral(operateIntergralVo.getUserId(), operateIntergralVo.getValue());
        //int i = 1/0;
        if (count == 0) {
            throw new BusinessException(IntergralCodeMsg.INTERGRAL_NOT_ENOUGH);
        }
    }

    @Override
    public void incrIntegral(OperateIntergralVo operateIntergralVo) {
        //往积分支付t_account_log表中插入数据
        AccountLog accountLog = new AccountLog();
        accountLog.setAmount(operateIntergralVo.getValue());
        accountLog.setPkValue(operateIntergralVo.getPk());
        accountLog.setInfo(operateIntergralVo.getInfo());
        accountLog.setGmtTime(new Date());
        accountLog.setType(AccountLog.TYPE_INCR);
        //增加积分
        usableIntegralMapper.addIntergral(operateIntergralVo.getUserId(), operateIntergralVo.getValue());
    }

    @Override
    public void incrIntergralTry(OperateIntergralVo operateIntergralVo, BusinessActionContext context) {
        log.info("进入Try");
        insertTransaction(operateIntergralVo, context, AccountTransaction.STATE_TRY, AccountTransaction.INCR_TYPE);
    }

    public void insertTransaction(OperateIntergralVo operateIntergralVo, BusinessActionContext context, int state, String type) {
        AccountTransaction accountTransaction = new AccountTransaction();
        accountTransaction.setActionId(context.getBranchId() + "");
        accountTransaction.setAmount(operateIntergralVo.getValue());
        accountTransaction.setGmtCreated(new Date());
        accountTransaction.setGmtModified(new Date());
        accountTransaction.setTxId(context.getXid());
        accountTransaction.setUserId(operateIntergralVo.getUserId());
        accountTransaction.setState(state);
        accountTransaction.setType(type);
        accountTransactionMapper.insert(accountTransaction);
    }

    @Override
    public void incrIntergralCommit(BusinessActionContext context) {
        log.info("进入Commit");
        AccountTransaction accountTransaction = accountTransactionMapper.get(context.getXid(), context.getBranchId() + "");
        if (accountTransaction == null) {
            log.info("出现异常");
        }
        if (AccountTransaction.STATE_TRY.equals(accountTransaction.getState())) {
            //向account_transaction表中插入一条数据
            OperateIntergralVo operateIntergralVo = JSON.parseObject(context.getActionContext("operateIntergralVo").toString(), OperateIntergralVo.class);
            //正常执行commit逻辑
            usableIntegralMapper.addIntergral(operateIntergralVo.getUserId(), operateIntergralVo.getValue());
            //把状态改为已提交状态
            accountTransactionMapper.updateAccountTransactionState(accountTransaction.getTxId(), context.getBranchId() + "", AccountTransaction.STATE_COMMIT, AccountTransaction.STATE_TRY);
        } else if (AccountTransaction.STATE_COMMIT.equals(accountTransaction.getState())) {
            //什么都不做，处理幂等性
        } else {
            log.info("出现异常");
        }
    }

    @Override
    public void incrIntergralRollback(BusinessActionContext context) {
        log.info("进入Rollback");
        AccountTransaction accountTransaction = accountTransactionMapper.get(context.getXid(), context.getBranchId() + "");
        if (accountTransaction == null) {
            //向account_transaction表中插入一条数据
            OperateIntergralVo operateIntergralVo = JSON.parseObject(context.getActionContext("operateIntergralVo").toString(), OperateIntergralVo.class);
            insertTransaction(operateIntergralVo, context, AccountTransaction.STATE_CANCEL, AccountTransaction.INCR_TYPE);
        }
        if (AccountTransaction.STATE_TRY.equals(accountTransaction.getState())) {
            //执行cancel逻辑
            //把状态改成回滚状态
            accountTransactionMapper.updateAccountTransactionState(accountTransaction.getTxId(), context.getBranchId() + "", AccountTransaction.STATE_CANCEL, AccountTransaction.STATE_TRY);
        } else if (AccountTransaction.STATE_CANCEL.equals(accountTransaction.getState())) {
            //什么都不做，处理幂等性
        } else {
            log.info("出现异常");
        }
    }

    @Override
    public void decrIntergralTry(OperateIntergralVo operateIntergralVo, BusinessActionContext context) {
        log.info("进入Try");
        insertTransaction(operateIntergralVo, context, AccountTransaction.STATE_TRY, AccountTransaction.DECR_TYPE);
        //冻结金额
        usableIntegralMapper.freezeIntergral(operateIntergralVo.getUserId(), operateIntergralVo.getValue());
    }

    @Override
    public void decrIntergralCommit(BusinessActionContext context) {
        log.info("进入Commit");
        AccountTransaction accountTransaction = accountTransactionMapper.get(context.getXid(), context.getBranchId() + "");
        if (accountTransaction == null) {
            log.info("出现异常");
        }
        if (AccountTransaction.STATE_TRY.equals(accountTransaction.getState())) {
            //向account_transaction表中插入一条数据
            OperateIntergralVo operateIntergralVo = JSON.parseObject(context.getActionContext("operateIntergralVo").toString(), OperateIntergralVo.class);
            //正常执行commit逻辑
            usableIntegralMapper.decrIntergral(operateIntergralVo.getUserId(), operateIntergralVo.getValue());
            //提交修改扣除金额
            usableIntegralMapper.commitChange(operateIntergralVo.getUserId(), operateIntergralVo.getValue());
            //把状态改为已提交状态
            accountTransactionMapper.updateAccountTransactionState(context.getXid(), context.getBranchId() + "", AccountTransaction.STATE_COMMIT, AccountTransaction.STATE_TRY);
        } else if (AccountTransaction.STATE_COMMIT.equals(accountTransaction.getState())) {
            //什么都不做，处理幂等性
        } else {
            log.info("出现异常");
        }
    }

    @Override
    public void decrIntergralRollback(BusinessActionContext context) {
        log.info("进入Rollback");
        AccountTransaction accountTransaction = accountTransactionMapper.get(context.getXid(), context.getBranchId() + "");
        if (accountTransaction == null) {
            //向account_transaction表中插入一条数据
            OperateIntergralVo operateIntergralVo = JSON.parseObject(context.getActionContext("operateIntergralVo").toString(), OperateIntergralVo.class);
            insertTransaction(operateIntergralVo, context, AccountTransaction.STATE_CANCEL, AccountTransaction.DECR_TYPE);
            //释放预留的金额
            usableIntegralMapper.unFreezeIntergral(operateIntergralVo.getUserId(), operateIntergralVo.getValue());
        }
        if (AccountTransaction.STATE_TRY.equals(accountTransaction.getState())) {
            //执行cancel逻辑
            //把状态改成回滚状态
            accountTransactionMapper.updateAccountTransactionState(context.getXid(), context.getBranchId() + "", AccountTransaction.STATE_CANCEL, AccountTransaction.STATE_TRY);
        } else if (AccountTransaction.STATE_CANCEL.equals(accountTransaction.getState())) {
            //什么都不做，处理幂等性
        } else {
            log.info("出现异常");
        }
    }
}
